package com.zhn.evsi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhn.evsi.client.ContractsClient;
import com.zhn.evsi.domain.dto.evsi.contract.EvsiContractsDTO;
import com.zhn.evsi.domain.dto.evsi.contract.EvsiContractsItemsDTO;
import com.zhn.evsi.domain.dto.evsi.character.EvsiCharacterPublicInfoDTO;
import com.zhn.evsi.domain.entity.evsi.EvsiContracts;
import com.zhn.evsi.domain.entity.evsi.EvsiInvtypes;
import com.zhn.evsi.interceptor.CharacterInterceptor;
import com.zhn.evsi.mapper.EvsiContractsMapper;
import com.zhn.evsi.service.IEvsiContractsService;
import com.zhn.evsi.service.IEvsiInvtypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EvsiContractsServiceImpl extends ServiceImpl<EvsiContractsMapper, EvsiContracts> implements IEvsiContractsService {

    @Autowired
    private ContractsClient contractsClient;
    @Autowired
    private IEvsiInvtypesService invtypesService;
    @Autowired
    private CharacterInterceptor characterInterceptor;

    @Override
    public List<EvsiContractsDTO> myContracts() {
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        // 初始化集合
        List<EvsiContractsDTO> contractList = new ArrayList<>();
        // 获取合同
        for (int i=1; i<100; i++) {
            List<EvsiContractsDTO> contracts = contractsClient.contracts(characters.getCharacters(), i);
            contractList.addAll(contracts);
            if (contracts.size() < 100) {
                break;
            }
        }
        contractList.forEach(v -> {
            List<EvsiContractsItemsDTO> evsiContractsItems = contractsClient.contractsItems(characters.getCharacters(), v.getContract_id());
            evsiContractsItems.forEach(k -> {
                EvsiInvtypes invtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, k.getType_id()));
                k.setType_name(invtypes.getTypeName());
            });
            v.setContractsItemsDTOS(evsiContractsItems);
        });
        return contractList;
    }
}
